#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <memory>
#include "thread.hpp"


uint16_t serverport = 0;
std::string serverip;
static void usage(std::string proc)
{
    std::cout << "\nUsage: " << proc << " ServerIP ServerPort \n" << std::endl;
}

static void* udpSend(void* args)
{
    int sock = *((int*)((ThreadData*)args)->args_);
    std::string name = ((ThreadData*)args)->name_;
    std::string message;
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

    while(true)
    {
        std::cerr << "请输入你的信息->" << std::endl;
        std::getline(std::cin,message);
        if(message == "quit")
        {
            break;  
        }
        // 当client首次发送消息给服务器的时候，OS会自动给client bind他的IP和PORT
        sendto(sock,message.c_str(),message.size(),0,(sockaddr*)&server,sizeof (server));
    }
    return nullptr;
}

static void* udpRecv(void* args)
{
    int sock = *(int *)((ThreadData *)args)->args_;
    std::string name = ((ThreadData *)args)->name_;

    char buffer[1024];
    while (true)
    {
        memset(buffer, 0, sizeof(buffer));
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        ssize_t s = recvfrom(sock, buffer, sizeof buffer, 0, (struct sockaddr *)&temp, &len);
        if (s > 0)
        {
            buffer[s] = 0;
            std::cout  << buffer << std::endl;
        }
    }
}

int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        usage(argv[0]);
        exit(1);
    }
    int sock = socket(AF_INET,SOCK_DGRAM,0);
    if(sock < 0)
    {
        std::cerr << " socket error " << std::endl;
        exit(2);
    }

    serverport = atoi(argv[2]);
    serverip = argv[1];
    std::unique_ptr<Thread> sender(new Thread(1,udpSend,(void*)&sock));
    std::unique_ptr<Thread> recver(new Thread(1,udpRecv,(void*)&sock));

    sender->start();
    recver->start();


    sender->join();
    recver->join();

    close(sock);

    //client要不要绑定？？要的，但是一般不需要显示的绑定，程序员不会自己bind
    //client是一个客户端，普通人下载和启动的，如果程序员自己bind的了，client一定bind
    //了一个固定的ip和port，万一，其他客户端提前占用了这个port呢？？
    //client一般不需要提前显示的bind指定port，而是让OS自行选择，这样才不会有冲突问题
    // std::string message;
    // struct sockaddr_in server;
    // memset(&server, 0, sizeof(server));
    // server.sin_family = AF_INET;
    // server.sin_port = htons(atoi(argv[2]));
    // server.sin_addr.s_addr = inet_addr(argv[1]);

    // char buffer[1024];
    // while(true)
    // {
    //     std::cout << "请输入你的信息->" << std::endl;
    //     std::getline(std::cin,message);
    //     if(message == "quit")
    //     {
    //         break;
    //     }
    //     // 当client首次发送消息给服务器的时候，OS会自动给client bind他的IP和PORT
    //     sendto(sock,message.c_str(),message.size(),0,(sockaddr*)&server,sizeof (server));

    //     //读取数据
    //     struct sockaddr_in temp;
    //     socklen_t len = sizeof(temp);
    //     ssize_t s = recvfrom(sock,buffer,sizeof(buffer), 0, (sockaddr*)&temp,&len);
    //     if(s > 0)
    //     {
    //         buffer[s] = 0;
    //         std::cout << "client echo# " << buffer << std::endl;
    //     }
    // }

    // close(sock);
    return 0;
}